﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EncuentraWEB
{
    public partial class Formulario_web19 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                if (!IsPostBack)
                {
                    cargarTreeView();
                }
                var scriptPopUp = new System.Text.StringBuilder();
                scriptPopUp.Append("PABLO();");
                ScriptManager.RegisterStartupScript(this, this.GetType(), "VENTANAPopUp", scriptPopUp.ToString(), true);
            
            
        }

        private void cargarTreeView()
        {
        //    Dim strsql As String
        //Dim cn As New clsGrlConeccion
        //Dim rs As System.Data.SqlClient.SqlDataReader
        //Dim cmd As New System.Data.SqlClient.SqlCommand()
        //Dim emp, lin, coc As String
        //Dim n, n1, n2 As New TreeNode
        //TreeView
        int childnum=0;
        int i=0;


        var encuentraContext = new EncuentraEntities();
        encuentraContext.Connection.Open();
       var empresasquery = from emp in encuentraContext.Empresas
                                select new { emp.intIdEmpresa, emp.varDenominacion};
            foreach (var emp in empresasquery)
            {
                int empresa = emp.intIdEmpresa;
                var cochesquery = from coc in encuentraContext.Coches
                                    where coc.Empresas.intIdEmpresa == empresa
                                    select new { coc.intIdCoche, coc.varDominio};
                //int n = TreeView1.Nodes.Add(emp, emp, i);
                TreeNode n = new TreeNode(emp.varDenominacion,emp.intIdEmpresa.ToString(), "~//Imagenes//busnaranja16x16.png");
                n.Checked = true;
                TreeView1.Nodes.Add(n);
                i = TreeView1.Nodes.IndexOf(n);
                foreach (var coc in cochesquery)
                {
                    TreeNode child = new TreeNode(coc.varDominio, "coc" + coc.intIdCoche.ToString());
                    child.Checked = true;
                    TreeView1.Nodes[i].ChildNodes.Add(child);

                }
            }


            //strsql = "Select  cocID,empRazonSocial, linDescripcion, cocDescripcion " & _
            //         " From  Coches C " & _
            //         " Inner Join Lineas L On L.linID = C.linId " & _
            //         " Inner Join Empresas Em On C.empID = Em.empId order by Em.empID, linDescripcion, cocDescripcion "
            //rs = cn.EjecutarSqlDataReader(strsql)
            //emp = ""
            //lin = ""
            //coc = ""
            //TreeView1.CheckBoxes = True
            //i = 0
            //While rs.Read()
            //    If emp <> rs("empRazonSocial") Then
            //        emp = rs("empRazonSocial")
            //       
            //    End If
            //    If lin <> rs("linDescripcion") Then
            //        lin = rs("linDescripcion")
            //        n1 = TreeView1.Nodes(n.Index).Nodes.Add(lin, lin, i - 1)
            //    End If
            //    coc = rs("cocDescripcion")
            //    n2 = TreeView1.Nodes(n.Index).Nodes(n1.Index).Nodes.Add(coc, coc, i - 1)
            //    n2.ContextMenuStrip = ContextMenuStrip1
            //    n2.Tag = rs("cocID")
            //End While
            //childnum = TreeView1.Nodes.Count
            //For i = 0 To childnum - 1
            //    TreeView1.Nodes(i).Checked = True
            //Next
            //rs.Close()
            //cn.Cerrar()
        }

    }
}
